package com.carrot.newsgallery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrot.newsgallery.entity.NewsMain;
import com.carrot.newsgallery.mapper.NewsMainMapper;
import com.carrot.newsgallery.service.NewsMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class NewsMainServiceImpl extends ServiceImpl<NewsMainMapper, NewsMain> implements NewsMainService {

    @Autowired
    private NewsMainMapper newsMainMapper;

    private static String[] hours = {" 04"," 10"," 16"," 22"};

    @Override
    public List<NewsMain> getToday() {
        LambdaQueryWrapper<NewsMain> lqw = new LambdaQueryWrapper<>();
        String date = LocalDate.now().toString();
        String yesterday = LocalDate.now().minusDays(1).toString();
        String hour = LocalDateTime.now().toString().substring(11,13);
        int hour_int = Integer.parseInt(hour);
        if (hour_int < 5)
            date = yesterday + hours[3];
        else if (hour_int < 11)
            date += hours[0];
        else if (hour_int < 17)
            date += hours[1];
        else if (hour_int < 23)
            date += hours[2];
        else
            date += hours[3];

        lqw.like(true,NewsMain::getCreateDate, date);
        return newsMainMapper.selectList(lqw);
    }

    @Override
    public List<NewsMain> getHistory(String date, int batch) {
        LambdaQueryWrapper<NewsMain> lqw = new LambdaQueryWrapper<>();
        date += hours[batch];
        lqw.like(true,NewsMain::getCreateDate, date);
        return newsMainMapper.selectList(lqw);
    }
}
